//
//  YLAttributedStr.m
//  testYYKit
// QQ:896525689
// Email:zhangyuluios@163.com
//                 _
// /\   /\        | |
// \ \_/ / _   _  | |     _   _
//  \_ _/ | | | | | |    | | | |
//   / \  | |_| | | |__/\| |_| |
//   \_/   \__,_| |_|__,/ \__,_|
//  Created by shuogao on 16/8/24.
//  Copyright © 2016年 Yulu Zhang. All rights reserved.
//

#import "YLAttributedStr.h"
#import <CoreText/CoreText.h>
@implementation YLAttributedStr

#pragma mark -- 设置在一个文本中所有特殊字符的特殊颜色

+ (__kindof NSMutableAttributedString *_Nonnull)changeSpecifiStrWithTotalString:(NSString *_Nonnull)totalStr andSpecifiStr:(NSString *_Nonnull)specifiStr withColor:(UIColor *_Nonnull)color specifiStrFont:(UIFont *_Nonnull)font {

    NSMutableAttributedString *mutableAttributedStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
    //    NSArray *array = [allStr componentsSeparatedByString:specifiStr];//array.cout-1是所有字符特殊字符出现的次数
     NSRange searchRange = NSMakeRange(0, [totalStr length]);
     NSRange range;
    //拿到所有的相同字符的range
     while
         ((range = [totalStr rangeOfString:specifiStr options:0 range:searchRange]).location != NSNotFound) {
         //改变多次搜索时searchRange的位置
         searchRange = NSMakeRange(NSMaxRange(range), [totalStr length] - NSMaxRange(range));
         //设置富文本
         [mutableAttributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
         [mutableAttributedStr addAttribute:NSFontAttributeName value:font range:range];
     }
     return mutableAttributedStr;
}

#pragma mark -- 改变一个文本中所有数字的颜色

+ (__kindof NSMutableAttributedString *_Nonnull)changNumberColorWithTotalString:(NSString *_Nonnull)totalString andColor:( UIColor * _Nonnull )numberColor {

    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];

    //拿到所有的相同字符的range
    NSArray *numberArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];


    for (int i = 0; i < totalString.length; i ++) {
        //每次只截取一个字符的范围
        NSString *oneStr = [totalString substringWithRange:NSMakeRange(i, 1)];
        //判断装有0-9的字符串的数字数组是否包含截取字符串出来的单个字符，从而筛选出符合要求的数字字符的范围NSMakeRange
        if ([numberArray containsObject:oneStr]) {

            [attributedStr addAttribute:NSForegroundColorAttributeName value:numberColor range:NSMakeRange(i, 1)];
            
        }
    }

    return attributedStr;
}

#pragma mark -- 让一个数组字符串改变颜色、字体

+ (__kindof NSMutableAttributedString *_Nonnull)changeSubStringWithTotalString:(NSString *_Nonnull)totalStr subStringColor:(UIColor *_Nonnull)color subStringArray:(NSArray *_Nonnull)subArray andFont:(UIFont *_Nonnull)font {

    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalStr];

    for(NSString *rangeStr in subArray) {

        NSRange range = [totalStr rangeOfString:rangeStr options:NSBackwardsSearch];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
        [attributedStr addAttribute:NSFontAttributeName value:font range:range];
        }
    return attributedStr;
}
#pragma mark -- 让一个字符串中的某个字符改变颜色、字体
+(NSMutableAttributedString *)changeSubStringWithTotalString:(NSString *)totalStr subStringColor:(UIColor *)color subString:(NSString *)substr andFont:(UIFont *)font
{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
    if (substr) {
        NSRange range = [totalStr rangeOfString:substr options:NSBackwardsSearch];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
        [attributedStr addAttribute:NSFontAttributeName value:font range:range];
    }
    
    return attributedStr;
}
#pragma mark -- 改变字间距

+ (__kindof NSMutableAttributedString *_Nonnull)changeSpaceWithTotalString:(NSString *_Nonnull)totalString textSpace:(CGFloat)textSpace {

    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    long number = textSpace;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    [attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
    CFRelease(num);
    return attributedStr;
}

#pragma mark -- 改变行间距

+ (__kindof NSMutableAttributedString *_Nonnull)changeRowSpaceWithTotalString:(NSString *_Nonnull)totalString rowSpace:(CGFloat)rowSpace {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:rowSpace];
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
    return attributedStr;
}

#pragma mark -- 改变行和字间距

+ (__kindof NSMutableAttributedString *_Nonnull)changeRowAndTextSpaceWithTotalString:(NSString *_Nonnull)totalString rowSpace:(CGFloat)rowSpace textSpace:(CGFloat)textSpace {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:rowSpace];
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
    long number = textSpace;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    [attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
    CFRelease(num);
    return attributedStr;
}



@end
